package com.mrchip.datecomponent;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AnalogClock;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DigitalClock;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.lang.annotation.Target;
import java.text.DateFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private Button btDate;
    private Button btTime;
    private TimePicker timePicker;
    private DatePicker datePicker;
    private TextView date;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btDate = findViewById(R.id.btDatePickerDialog);
        btTime = findViewById(R.id.btTimePickerDialog);
        timePicker = findViewById(R.id.timePicker);
        date = findViewById(R.id.dateText);
        datePicker = findViewById(R.id.datePicker);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                @Override
                public void onTimeChanged(TimePicker timePicker, int i, int i1) {
                    int hour = timePicker.getHour();
                    int minute = timePicker.getMinute();
                    boolean is24Hour = timePicker.is24HourView();
                    Log.d("TimePicker", hour+":"+minute);
                    Toast.makeText(getBaseContext(), hour + ":" + minute, Toast.LENGTH_SHORT).show();
                }

            });

            datePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
                @Override
                public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
                    int year = datePicker.getYear();
                    int mon = datePicker.getMonth();
                    int day = datePicker.getDayOfMonth();
                    date.setText(year+"-"+mon+"-"+day);
                    Log.d("TimePicker", year+"-"+mon+"-"+day);
                    Toast.makeText(getBaseContext(), year+"-"+mon+"-"+day, Toast.LENGTH_SHORT).show();
                }
            });

            btDate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Calendar cal=Calendar.getInstance();
                    int year=cal.get(Calendar.YEAR);       //获取年月日时分秒
                    int month=cal.get(Calendar.MONTH);   //获取到的月份是从0开始计数
                    int day=cal.get(Calendar.DAY_OF_MONTH);

                    DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, 0, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                            int year = datePicker.getYear();
                            int mon = datePicker.getMonth();
                            int day = datePicker.getDayOfMonth();
                            date.setText(year+"-"+mon+"-"+day);
                            Log.d("TimePickerDialog", year+"-"+mon+"-"+day);
                            Toast.makeText(getBaseContext(), year+"-"+mon+"-"+day, Toast.LENGTH_SHORT).show();
                        }
                    }, year, month, day);
                    datePickerDialog.show();
                }
            });

            btTime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final Calendar c = Calendar.getInstance();
                    int hour = c.get(Calendar.HOUR_OF_DAY);
                    int minute = c.get(Calendar.MINUTE);

                    TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker, int i, int i1) {
                            int hour = timePicker.getHour();
                            int minute = timePicker.getMinute();
                            boolean is24Hour = timePicker.is24HourView();
                            Log.d("TimePicker", hour+":"+minute);
                            Toast.makeText(getBaseContext(), hour + ":" + minute, Toast.LENGTH_SHORT).show();
                        }
                    }, hour, minute, false);
                    timePickerDialog.show();
                }
            });
        }





    }
}
